package cn.wolfcode.shop.controller;


import cn.wolfcode.shop.api.domain.OrderAction;
import cn.wolfcode.shop.api.domain.OrderInfo;
import cn.wolfcode.shop.api.domain.OrderProduct;
import cn.wolfcode.shop.api.query.OrderInfoQueryObject;
import cn.wolfcode.shop.api.query.PageResult;
import cn.wolfcode.shop.api.service.IOrderActionService;
import cn.wolfcode.shop.api.service.IOrderProductService;
import cn.wolfcode.shop.api.service.IOrderinfoService;
import cn.wolfcode.shop.api.util.JsonResult;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("orderinfo")
public class OrderInfoController {

    @Reference
    private IOrderinfoService orderinfoService;
    @Reference
    private IOrderProductService orderProductService;
    @Reference
    private IOrderActionService orderActionService;

    @RequestMapping("getAll")
    @ResponseBody
    public List<OrderInfo> getAll(){
        return orderinfoService.getAll();
    }

    @RequestMapping("/orderInfolist")
    public String orderInfoPage(Model model, @ModelAttribute("qo")OrderInfoQueryObject qo){
        PageResult pageResult = orderinfoService.query(qo);
        model.addAttribute("pageResult", pageResult);
        return "orderInfo/orderInfolist";
    }
    /**
     * 订单详细页面
     */
    @RequestMapping("/orderDerdetail")
    public String orderDerdetail(Long orderId, Model model){
        OrderInfo orderDerdetail = orderinfoService.get(orderId);
        List<OrderProduct> products = orderProductService.selectProductByOrderId(orderDerdetail.getId());
        List<OrderAction> orderActions = orderActionService.selectActionByOrderId(orderDerdetail.getId());
        model.addAttribute("orderActions", orderActions);
        model.addAttribute("orderDerdetail", orderDerdetail);
        model.addAttribute("products", products);
        return "orderInfo/orderdetail";
    }
    /**
     * 订单确认列表
     */
    @RequestMapping("/orderInfoVerify")
    public String orderInfoVerify(Model model, @ModelAttribute("qo") OrderInfoQueryObject qo){
        qo.setOrderState(OrderInfo.ORDER_NORMAL);
        model.addAttribute("pageResult", orderinfoService.query(qo));
        return "/orderInfo/orderInfoverify";
    }

    @RequestMapping("/orderVerifyView")
    public String orderVerifyView(Long orderId, Model model){
        OrderInfo orderDerdetail = orderinfoService.get(orderId);
        List<OrderProduct> products = orderProductService.selectProductByOrderId(orderDerdetail.getId());
        List<OrderAction> orderActions = orderActionService.selectActionByOrderId(orderDerdetail.getId());
        model.addAttribute("orderActions", orderActions);
        model.addAttribute("orderDerdetail", orderDerdetail);
        model.addAttribute("products", products);
        return "orderInfo/orderVerifyView";
    }
    /**
     * 订单发货列表
     */
    @RequestMapping("/orderInfoShipp")
    public String orderInfoShipp(Model model, @ModelAttribute("qo") OrderInfoQueryObject qo){
        qo.setOrderState(OrderInfo.ORDER_CONFIRM);
        qo.setShippState(OrderInfo.SHOP_NORMAL);
        model.addAttribute("pageResult", orderinfoService.query(qo));
        return "orderInfo/orderInfoShipp";
    }
    @RequestMapping("/orderShippView")
    public String orderShippView(Long orderId, Model model){
        OrderInfo orderDerdetail = orderinfoService.get(orderId);
        List<OrderProduct> products = orderProductService.selectProductByOrderId(orderDerdetail.getId());
        List<OrderAction> orderActions = orderActionService.selectActionByOrderId(orderDerdetail.getId());
        model.addAttribute("orderActions", orderActions);
        model.addAttribute("orderDerdetail", orderDerdetail);
        model.addAttribute("products", products);
        return "orderInfo/orderShippView";
    }

    /** 订单的操作信息
     * http://localhost:8081/orderinfo/changeOrderStatus
     */
    @RequestMapping("/changeOrderStatus")
    @ResponseBody
    public JsonResult changeOrderStatus(Long id, Byte state, String remark, String deliverSn){
        JsonResult result = null;
        try {
             orderinfoService.changeOrderStatus(id, state, remark, deliverSn);
            result = new JsonResult();
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult(false, e.getMessage() );
        }
        return result;
    }
    /**
     * 商品详细更新
     */
    @RequestMapping("/productUpdate")
    @ResponseBody
    public JsonResult productUpdate(OrderInfo orderInfo){
        JsonResult result = null;
        try {
            orderinfoService.productUpdate(orderInfo);
            result = new JsonResult();
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult(false, e.getMessage() );
        }
        return result;
    }
    /**
     *  更新购货人
     */
    @RequestMapping("/orderInfoUpdate")
    @ResponseBody
    public JsonResult orderInfoUpdate(OrderInfo orderInfo){
        JsonResult result = null;
        try {
            orderinfoService.orderInfoUpdate(orderInfo);
            result = new JsonResult();
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult(false, e.getMessage() );
        }
        return result;
    }

    /**
     *
     * 退货列表
     */
    @RequestMapping("/productReturn")
    public String productReturn(Model model, Long id ,  @ModelAttribute("qo")OrderInfoQueryObject qo) {
        qo.setOrderState(OrderInfo.SHOP_CONFIRM);  //已发货
        PageResult pageResult = orderinfoService.query(qo);
        model.addAttribute("pageResult", pageResult);
        return "/orderInfo/productReturn";
    }

    /**
     *
     * 退货商品列表
     */
    @RequestMapping("/productReturnDetail")
    public String productReturnDetail(Model model, Long id ,  @ModelAttribute("qo")OrderInfoQueryObject qo) {
        qo.setOrderState(OrderInfo.SHOP_CONFIRM);  //已发货
        PageResult pageResult = orderinfoService.query(qo);
        model.addAttribute("pageResult", pageResult);
        List<OrderAction> orderActions = orderActionService.selectActionByOrderId(id);
        model.addAttribute("orderActions", orderActions);
        List<OrderProduct> orderProducts = orderProductService.selectProductByOrderId(id);
        model.addAttribute("orderProducts", orderProducts);
        int number = orderProductService.countProductAmountByOrderId(id);
        model.addAttribute("number", number);

        BigDecimal totalMount = new BigDecimal(0);
        for (OrderProduct orderProduct : orderProducts) {
            totalMount = totalMount.add(orderProduct.getAmount());
        }
        model.addAttribute("totalMount",totalMount);

        return "/orderInfo/productReturnDetail";


    }




}
